/**
 * 
 */
package com.bitBusy.manageyourriches.utilityClasses.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bitBusy.manageyourriches.R;
import com.bitBusy.manageyourriches.alarmSystem.AlarmManagement;
import com.bitBusy.manageyourriches.database.contactClasses.PaymentReminder;
import com.bitBusy.manageyourriches.database.managers.DateTimeManager;
import com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBoxPrompts;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayButtonValueUsers;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayMessages;

/**
 * @author SumaHarsha
 *
 */
public class ScheduledExportsAdapter extends BaseAdapter implements DisplayButtonValueUsers{

	
	
	private static final String DELETE = "Delete";
	
	private static final String DELETEDREMINDER = "Scheduled export has been deleted";
	
	 private static final String WEEKLYPERIOD = "weekly";
	 
	 private static final String MONTHLYPERIOD = "monthly";

		private static final String EDIT = "Hi";
	
	/** keyset array*/
	private ArrayList<?> myReminders;
	
	private Context myContext;
	
	private int myPosition;
	
	public ScheduledExportsAdapter(Context the_context, ArrayList<?> the_reminders) {
		myContext = the_context;
		myReminders = the_reminders;
	}


	public int getCount() {
		return (myReminders==null?0:myReminders.size());
	}

	public Object getItem(int position) {
	return (myReminders != null)?myReminders.get(position):null;
	}

	public long getItemId(int position) {
			return position;
	}

	public View getView(final int position, View the_convertView, ViewGroup parent) {
		
		  if (the_convertView == null) {
		   LayoutInflater inf = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		   the_convertView = inf.inflate(R.layout.scheduledexports_rowformat, null);
		   }
	
		Object item = getItem(position);
			TextView date = (TextView) the_convertView.findViewById(R.id.sch_exports_date);
			TextView period = (TextView) the_convertView.findViewById(R.id.sch_exports_period);
			TextView email = (TextView) the_convertView.findViewById(R.id.sch_exports_email);

			ImageButton editButton = (ImageButton) the_convertView.findViewById(R.id.sch_exports_editDeleteImg);

			
			if (item instanceof PaymentReminder && item != null)
			{		
				PaymentReminder reminder = (PaymentReminder)item;
				date.setText(DateTimeManager.getDate(reminder.getDay(), reminder.getMonth()+1, reminder.getYear()));
				period.setText(reminder.isMonthly() == 1?MONTHLYPERIOD:WEEKLYPERIOD);
				email.setText(reminder.getEmail());
				setEditListener(editButton, position, this);
				if (position % 2 == 1)
				{
					date.setBackgroundColor(Color.LTGRAY);
					period.setBackgroundColor(Color.LTGRAY);
					email.setBackgroundColor(Color.LTGRAY);
				}
			}
			else if (item instanceof ScheduledExportsTitle && item != null)
			{
				ScheduledExportsTitle title = (ScheduledExportsTitle)item;
				date.setText(title.getDateHeader());
				date.setTextColor(Color.RED);
				date.setBackgroundColor(Color.TRANSPARENT);
				period.setText(title.getPeriodHeader());
				period.setTextColor(Color.RED);
				period.setBackgroundColor(Color.TRANSPARENT);
				email.setText(title.getEmailHeader());
				email.setTextColor(Color.RED);
				email.setBackgroundColor(Color.TRANSPARENT);
			
				editButton.setVisibility(View.INVISIBLE);
		}
			return the_convertView;
	}


	

	
	/**
	 * @param day
	 * @param month
	 * @param year
	 * @return
	 */
	

	/* (non-Javadoc)
	 * @see com.bitBusy.manageyourriches.utilityClasses.display.DisplayButtonValueUsers#getButtonClickOnDialog(android.widget.Button)
	 */
	@Override
	public void getButtonClickOnDialog(Button the_button) {
		if (the_button != null)
		{
			String buttonText = the_button.getText().toString();
			Object item = myReminders.get(myPosition);
			PaymentReminder reminder =  item instanceof PaymentReminder?(PaymentReminder) item:null;
			if (buttonText.equals(EDIT))
				{
					Toast.makeText(myContext.getApplicationContext(), EDIT, Toast.LENGTH_SHORT).show();
				}
				
				if (buttonText.equals(DELETE))
				{
					if(deleteReminder(reminder))
					{
						myReminders.remove(myPosition);
					}
					Toast.makeText(myContext.getApplicationContext(), DELETEDREMINDER, Toast.LENGTH_LONG).show();

				}
				notifyDataSetChanged();				 	

			}
		}
		
		
	/**
	 * @param reminder
	 */
	private boolean deleteReminder(PaymentReminder the_reminder) {
		return AlarmManagement.deleteAlarm(the_reminder);
	}
	private void setEditListener(ImageButton the_button, final int the_position, final DisplayButtonValueUsers the_caller)
	{
		if (the_button != null)
		{
			
			the_button.setOnClickListener(new OnClickListener() { 
				@Override
				public void onClick(View the_view) {
						myPosition = the_position;
					  DisplayMessages.displayDialogWithThreeButtonsOnly(myContext, the_caller,
							  DialogBoxPrompts.DIALOGEDITREMINDERTITLE.getPrompt(), DELETE,EDIT, DialogBoxPrompts.DIALOGCANCELPROMPT.getPrompt());
				}

			});
		}
	} 
	
	
	/**Class for title*/
	 public static class ScheduledExportsTitle
	{
		
		/**Date header*/
		private static String myDateHeader;
		
		/**Payment Info Header*/
		private static String myPeriodHeader;
		
		private static String myEmailHeader;
		
		/**
		 * Constructor
		 * @param the_dateTimeHeader
		 * @param the_paymentInfoHeader
		 * @param the_amountHeader
		 * @param the_noteHeader
		 */
		 public ScheduledExportsTitle(String the_dateHeader, String the_periodHeader, String the_emailHeader)
		 {
			 myDateHeader = the_dateHeader;
			 myPeriodHeader = the_periodHeader;
			 myEmailHeader = the_emailHeader;
		 }
		 
		 /**Getters*/
		 
		 public String getDateHeader()
		 {
			 return myDateHeader;
		 }
		 
			 public String getPeriodHeader()
		 {
			 return myPeriodHeader;
		 }
			 public String getEmailHeader()
			 {
				 return myEmailHeader;
			 }
	}
}
